import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/bean/opetation_detail.dart';
import 'package:hygf/page/home/operation/task/view.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import '../../../../config/RouteConfig.dart';
import '../../../../config/StyleConfig.dart';
import '../../../../res/HDColors.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/empty_data_widget.dart';
import 'logic.dart';

class OprecordPage extends StatelessWidget {
  OprecordPage({Key? key}) : super(key: key);

  final logic = Get.put(OprecordLogic());
  final state = Get.find<OprecordLogic>().state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          logic.closePage();
          return false;
        },
        child: Scaffold(
          appBar: AppBar(
              title: const Text("运维记录"),
              elevation: 0,
              backgroundColor: HDColors.green,
              leading: CommonWidget.getAppBarLeading()),
          body: GetBuilder<OprecordLogic>(builder: (logic) {
            return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: CommonWidget.searchTitle(
                            state.searchController, (value) => {}, [], "",
                            searchHintText: "请输入手机号/客户姓名",
                            onSubmitted: (value) {
                              logic.requestList(true,searchKey: value);
                            }),
                      ),
                    ],
                  ),
                  CommonWidget.divider(0.5),
                  SizedBox(
                    height: 10,
                  ),
                  Expanded(child: buildList())
                ]);
          }),
        ));
  }

  /// 数据列表
  buildList() {
    return GetBuilder<OprecordLogic>(builder: (logic) {
      return EasyRefresh(
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          controller: state.controller,
          emptyWidget: state.dataList.isEmpty ? const EmptyDataWidget() : null,
          onRefresh: () async {
            state.noMore = true;
            state.controller.finishRefresh();
            logic.requestList(true);
          },
          onLoad: state.noMore
              ? null
              : () async {
            logic.requestList(false);
          },
          child: ListView.separated(
            shrinkWrap: true,
            itemCount: state.dataList.length,
            itemBuilder: (BuildContext context, int index) {
              return buildCheckItem(index, state.dataList);
            },
            separatorBuilder: (BuildContext context, int index) {
              return Container(height: 10);
            },
          ));
    });
  }

  buildCheckItem(index, List<dynamic> dataList) {
    OperationBean itemBean = dataList[index];
    var tempColor = HDColors.green;
    var temImg = "assets/images/fix.png";
    double tempSize = 40;
    String text = "抢修";
    String buttonTxt = "处理";
    return InkWell(
        onTap: () {
          var args = {"pageData": itemBean, "title": "运维记录"};
          var result = HDNavigator.openToName(RouteConfig.operationDetail,
              arguments: args);
          if (result != null && result == "refresh") {
            state.controller.callRefresh();
          }
        },
        child: Card(
          margin: EdgeInsets.fromLTRB(20, 5, 20, 0),
          color: HDColors.white,
          shadowColor: Colors.amber,
          elevation: 1,
          shape: RoundedRectangleBorder(
              side: BorderSide(width: 0.3, color: HDColors.divider),
              borderRadius: BorderRadius.all(new Radius.circular(5))),
          child: Container(
              width: double.infinity,
              height: 148,
              child: Stack(children: [
                Container(
                  margin: EdgeInsets.fromLTRB(20, 25, 20, 20),
                  child: ClipRRect(
                      borderRadius: BorderRadius.circular(5.0),
                      child: Stack(
                        alignment: Alignment.topCenter,
                        children: [
                          Opacity(
                            opacity: 0.5,
                            child: Container(
                              color: tempColor,
                              width: 60,
                              height: 60,
                            ),
                          ),
                          Column(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Image.asset(
                                temImg,
                                fit: BoxFit.scaleDown,
                                width: tempSize,
                                height: tempSize,
                              ),
                              Text(
                                text,
                                style: TextStyle(
                                    fontSize: StyleConfig.fontContentSize14,
                                    color: tempColor),
                              ),
                            ],
                          )
                        ],
                      )),
                ),
                Container(
                    margin: EdgeInsets.fromLTRB(100, 20, 0, 0),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(itemBean.customerName ?? "",
                              style: TextStyle(
                                  fontSize: StyleConfig.fontContentSize16,
                                  color: HDColors.black,
                                  fontWeight: FontWeight.bold)),
                          SizedBox(height: 3), //
                          Text(itemBean.createDate ?? "",
                              style: TextStyle(
                                  fontSize: StyleConfig.fontContentSize14,
                                  color: HDColors.siGrayHint)),
                          SizedBox(height: 3),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Icon(
                                    color: HDColors.blue,
                                    Icons.location_on_outlined),
                                Expanded(
                                    child: Text(itemBean.buildingAddress ?? "",
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                            fontSize:
                                            StyleConfig.fontContentSize14,
                                            color: HDColors.siGrayHint))),
                              ]),
                          SizedBox(height: 3),
                          Text("踏勘位置：${itemBean.address}",
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: StyleConfig.fontContentSize14,
                                  color: HDColors.siGrayHint)),
                          Text("故障说明：${itemBean.faultDescription}",
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: StyleConfig.fontContentSize14,
                                  color: HDColors.siGrayHint)),
                        ])),
                Visibility(
                    visible: false,
                    child: Positioned(
                      top: 10,
                      right: 20,
                      child: CommonWidget.buildItemButton(
                          buttonTxt, tempColor, () {}),
                    ))
              ])),
        ));
  }
}
